<template>
  <el-cascader
    :value="value"
    :options="depts"
    :props="defaultProps"
    size="mini"
    separator="-"
    @change="changeValue"
  />
</template>

<script>
import { reqGetDepartment } from '@/api/department'
import { transListToTreeData } from '@/utils'
export default {
  props: {
    value: {
      type: Number,
      default: null
    }
  },
  data() {
    return {
      // 组织架构的数据数组
      depts: [],
      defaultProps: {
        label: 'name', // 告诉组件从哪个属性中取值去展示到节点上
        value: 'id' // 告诉组件从哪个属性中取值去提交
      }
    }
  },
  created() {
    this.getDepartment()
  },
  methods: {
    // 1. 获取组织架构数组
    async getDepartment() {
      this.depts = transListToTreeData(await reqGetDepartment(), 0)
    },
    // 2. 选择不同的部门
    changeValue(deptArr) {
      if (deptArr.length > 0) {
        this.$emit('input', deptArr[deptArr.length - 1])
      } else {
        this.$emit('input', null)
      }
    }
  }
}
</script>

<style>

</style>
